package com.entrogy.dao;

import com.alibaba.fastjson.JSONObject;
import com.entrogy.bean.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author 罗兴江
 * @date 2019/5/31 0024 下午 2:14
 * 考试dao接口
 */
@Repository
public interface ExamMapper {

    /**
     * 查询考试列表
     * @param teacherId
     * @param schoolCode
     * @param examId
     * @return
     */
    List<ExamEntity> findExamList(@Param(value = "teacherId") String teacherId, @Param(value = "schoolCode") Integer schoolCode, @Param("examId") Integer examId);

    /**
     * 向考试表内添加数据
     * @param examEntity
     * @return
     */
    int addExam(ExamEntity examEntity);

    /**
     * 添加要考试的科目
     * @param examId
     * @param courseIds
     * @return
     */
    int addExamCourse(@Param(value = "examId") Integer examId, @Param(value = "courseIds") List<Integer> courseIds);

    /**
     * 添加要考试的班级
     * @param examId
     * @param classIds
     * @return
     */
    int addExamClass(@Param(value = "examId")int examId, @Param(value = "classIds") List<Integer> classIds);

    /**
     * 查询考试列表
     * @param teacherId
     * @return
     */
    List<SimpleExamEntity> findSimpleExamList(@Param(value = "gradeId") Integer gradeId, @Param(value = "teacherId") String teacherId, @Param(value = "schoolCode") Integer schoolCode);

    /**
     * 初始化考试成绩，默认为空
     * @param examId
     * @param scoreTableName
     * @param studentId
     * @param courseList
     * @return
     */
    int insertInitExamScore(@Param(value = "examId") Integer examId, @Param(value = "scoreTableName")String scoreTableName, @Param(value = "studentId")Integer studentId, @Param(value = "courseList") List<CourseEntity> courseList);

    /**
     * 根据班级id集合查询所有属于该班级集合的学生
     * @param classIds
     * @return
     */
    List<Integer> findExamStuIdsByClassId(@Param("schoolCode") Integer schoolCode, @Param("classIds") List<Integer> classIds, @Param("examId") Integer examId);

    /**
     * 根据学生和考试id查询出他此次考试的科目id集合
     * @param schoolCode
     * @param studentId
     * @param examId
     * @return
     */
    List<Integer> findStuExamCourseIds(@Param("schoolCode") Integer schoolCode,@Param("studentId") Integer studentId, @Param("examId") int examId);

    /**
     * 通过学生要考试的静态班级查询学生考试的科目id
     * @param schoolCode
     * @param staticCourseIds
     * @param studentId
     * @param classIds
     * @return
     */
    List<CourseEntity> findStuExamCourseIdsByStaticCourseIds(@Param(value = "schoolCode") Integer schoolCode, @Param(value = "staticCourseIds") List<Integer> staticCourseIds, @Param(value = "studentId") Integer studentId, @Param(value = "classIds")List<Integer> classIds);

    List<Integer> findClassExamCourseIdsByStaticCourseIds(@Param(value = "schoolCode") Integer schoolCode, @Param(value = "staticCourseIds")List<Integer> courseIds, @Param(value = "gradeId") Integer gradeId, @Param(value = "classIds")List<Integer> classIds);

    List<GradeEntity> findExamGradeByClassIds(@Param("examClassIds") List<Integer> examClassIds, @Param(value = "examId") Integer examId);

    /**
     * 查询某一次考试要考的班级
     * @param examId
     * @return
     */
    List<Integer> findExamClassIds(Integer examId);

    /**
     * 根据考试id查询成绩存储的表名
     * @param examId
     * @return
     */
    String findScoreTableNameByExamId(Integer examId);

    /**
     * 查询该次考试的考试科目
     * @param examId
     * @return
     */
    List<Integer> findExamCourseIds(Integer examId);

    /**
     * 查询某次考试某科的排名
     * @param courseId
     * @param examId
     * @param scoreTableName
     * @return0
     */
    List<StudentCourseScoreEntity> findOrderByCourseScore(@Param(value = "courseId") Integer courseId, @Param(value = "examId") Integer examId, @Param(value = "scoreTableName") String scoreTableName);

    /**
     * 通过学生id查询考试的列表
     * @param stuId
     * @return
     */
    List<SimpleExamEntity> findExamListByStuId(Integer stuId);

    /**
     * 根据年级id或者教师id查询考试列表
     * @param gradeId
     * @param uId
     * @param schoolCode
     * @return
     */
    List<SimpleExamEntity> findSimpleExamListByGradeIdOrUid(@Param(value = "gradeId") String gradeId, @Param(value = "uId") String uId, @Param(value = "schoolCode") Integer schoolCode);

    /**
     * 根据考试id查询此次考试的的班级集合
     * @param examId
     * @return
     */
    List<Integer> listExamClassId(@Param("examId") Integer examId);

//    /**
//     * 根据普通教师查看的考试
//     * @param teacherId
//     * @return
//     */
//    List<SimpleExamEntity> findExamListForNormalTeacher(@Param(value = "teacherId") String teacherId);
//    /**
//     * 年级主任查看的考试
//     * @param gradeId
//     * @param schoolCode
//     * @return
//     */
//    List<SimpleExamEntity> findExamListForGradeLeader(@Param("gradeId") Integer gradeId, @Param("schoolCode") Integer schoolCode);
//
//    List<SimpleExamEntity> findExamListForClassLeader(String classId, Integer schoolCode);

    /**
     * 查看学校的所有考试
     * @param schoolCode
     * @param examName
     * @param examTime
     * @return
     */
    List<SimpleExamEntity> listSchoolExam(@Param("schoolCode") Integer schoolCode, @Param("examName") String examName, @Param("examTime") String examTime);

    /**
     * 初始化该次考试的排名记录
     * @param studentIds
     * @param examId
     * @return
     */
    Integer insertInitExamRank(@Param(value = "studentIds") List<Integer> studentIds, @Param(value = "examId") int examId);

    /**
     * 查询该次考试的基本信息
     * @param examId
     * @return
     */
    ExamEntity getExamInfo(Integer examId);

    /**
     * 查询某个班级拥有的所有静态科目
     * @param classId
     * @param schoolCode
     * @return
     */
    List<Integer> listClassStaticCourse(@Param(value = "classId") Integer classId, @Param(value = "schoolCode") Integer schoolCode);

    /**
     * 删除某次考试
     * @param examId
     * @return
     */
    int deleteExam(Integer examId);

    /**
     * 查询任课老师能录入成绩的考试列表
     * @param teacherId
     * @return
     */
    List<SimpleExamEntity> listExamForCourseTeacher(@Param("teacherId") String teacherId);

    /**
     * 计数
     * @param schoolCode
     * @return
     */
    int countExam(@Param("schoolCode") Integer schoolCode);

    /**
     * 计算某次考试的人数
     * @param examId
     * @return
     */
    Integer countExamStudent(@Param("examId") Integer examId);

    /**
     * 查询考试单科排名（全部学生）
     * @param staticId
     * @param examId
     * @param scoreTableName
     * @return
     */
    List<StudentCourseScoreEntity> listCourseGradeRank(@Param("staticId") String staticId, @Param("examId") Integer examId, @Param("scoreTableName") String scoreTableName);

    /**
     * 通过考试、科目id找到该班该科目的所有考试成绩
     * @param examId
     * @param examCourseId
     * @param scoreTableName
     * @return
     */
    List<Double> listExamCourseScore(@Param("examId") Integer examId, @Param("examCourseId") Integer examCourseId, @Param("scoreTableName") String scoreTableName);

    /**
     * 更新某一次考试某个科目的班级中位分
     * @param examId
     * @param examCourseId
     * @param courseClassMiddleScore
     * @return
     */
    int updateCourseClassMiddleScore(@Param("examId") Integer examId, @Param("examCourseId") Integer examCourseId, @Param("courseClassMiddleScore") double courseClassMiddleScore);

    /**
     * 通过考试、静态科目id找到该班该静态科目的所有考试成绩
     * @param examId
     * @param courseStaticId
     * @param scoreTableName
     * @return
     */
    List<Double> listExamCourseScoreForAll(@Param("examId") Integer examId, @Param("courseStaticId") String courseStaticId, @Param("scoreTableName") String scoreTableName);

    /**
     * 更新某一次考试某个科目的年级中位分
     * @param examId
     * @param examCourseId
     * @param courseGradeMiddleScore
     * @return
     */
    int updateCourseGradeMiddleScore(@Param("examId") Integer examId, @Param("examCourseId") Integer examCourseId, @Param("courseGradeMiddleScore") double courseGradeMiddleScore);

    /**
     * 得到某次考试某门科目的信息
     * @param examId
     * @param courseId
     * @return
     */
    ExamCourseInfoEntity getExamCourseInfo(@Param("examId") Integer examId, @Param("courseId") Integer courseId);

    /**
     * 查询出该次考试的所有学生
     * @param examId
     * @return
     */
    List<StudentBasicInfoEntity> listExamStudent(@Param("examId") Integer examId);

    /**
     * 查询该次考试的所有静态科目
     * @param examId
     * @return
     */
    List<CourseEntity> listExamCourse(@Param("examId") Integer examId);

    /**
     * 查询此次考试的所有班级
     * @param examId
     * @return
     */
    List<ClassEntity> listExamClass(@Param("examId") Integer examId);

    /**
     * 查询家长能够看哪些考试（自己的孩子的）
     * @param uId
     * @param examName
     * @param examTime
     * @return
     */
    List<SimpleExamEntity> listExamForParent(@Param("parentId") String uId, @Param("examName") String examName, @Param("examTime") String examTime);

    /**
     * 删除考试的排名生成记录
     * @param examId
     * @return
     */
    int deleteRankGenerateRecord(@Param("examId") Integer examId);

    /**
     * 插入考试的排名生成记录
     * @param examId
     * @param requireCourseClassRank
     * @param requireCourseGradeRank
     * @param requireTotalScoreClassRank
     * @param requireTotalScoreGradeRank
     * @param requireScienceOrArtsClassRank
     * @param requireScienceOrArtsGradeRank
     * @param requireClassAvgScore
     * @param requireGradeAvgScore
     * @return
     */
    boolean insertRankGenerateRecord(@Param("examId") Integer examId, @Param("requireCourseClassRank") Integer requireCourseClassRank,
                                     @Param("requireCourseGradeRank") Integer requireCourseGradeRank,
                                     @Param("requireTotalScoreClassRank") Integer requireTotalScoreClassRank,
                                     @Param("requireTotalScoreGradeRank") Integer requireTotalScoreGradeRank,
                                     @Param("requireScienceOrArtsClassRank") Integer requireScienceOrArtsClassRank,
                                     @Param("requireScienceOrArtsGradeRank") Integer requireScienceOrArtsGradeRank,
                                     @Param("requireClassAvgScore") Integer requireClassAvgScore,
                                     @Param("requireGradeAvgScore") Integer requireGradeAvgScore);

    /**
     * 查询该次考试的排名生成情况用于前端动态显示排名情况
     * @param examId
     * @return
     */
    JSONObject getRankGenerateRecord(Integer examId);

    /**
     * 初始化关于课程的排名信息
     * @param examId
     * @param scoreTableName
     * @return
     */
    int updateExamCourseScoreRank(@Param("examId") Integer examId, @Param("scoreTableName") String scoreTableName);

    /**
     * 初始化关于分数的排名信息
     * @param examId
     * @param scoreTableName
     * @return
     */
    int updateExamTotalScoreRank(@Param("examId") Integer examId, @Param("scoreTableName") String scoreTableName);

    /**
     * 批量更新学生的课程分数
     * @param studentCourseScoreList
     * @param scoreTableName
     */
    int batchUpdateScore(@Param("studentCourseScoreList") List<StudentCourseScoreEntity> studentCourseScoreList, @Param("scoreTableName") String scoreTableName);
}
